/*
 * Copyright (C) 2012 The * Project
 * All right reserved.
 * Version 1.00 2012-2-11
 * Author veally@foxmail.com
 */
package com.xindaoapp.happypet.view;

import java.util.Calendar;

import android.content.Context;
import android.database.ContentObserver;
import android.graphics.Color;
import android.os.Handler;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.widget.DigitalClock;

import com.xindaoapp.happypet.R;

/**
 *  Custom digital clock
 *
 */
@SuppressWarnings("deprecation")
public class CustomDigitalClock extends DigitalClock {

	Calendar mCalendar;
	private final static String m12 = "h:mm aa";
	private final static String m24 = "k:mm";
	private FormatChangeObserver mFormatChangeObserver;

	private Runnable mTicker;
	private Handler mHandler;
	private long endTime;
	private ClockListener mClockListener;

	private boolean mTickerStopped = false;

	@SuppressWarnings("unused")
	private String mFormat;

	public CustomDigitalClock(Context context) {
		super(context);
		initClock(context);
	}

	public CustomDigitalClock(Context context, AttributeSet attrs) {
		super(context, attrs);
		initClock(context);
	}

	private void initClock(Context context) {

		if (mCalendar == null) {
			mCalendar = Calendar.getInstance();
		}

		mFormatChangeObserver = new FormatChangeObserver();
		getContext().getContentResolver().registerContentObserver(Settings.System.CONTENT_URI, true, mFormatChangeObserver);

		setFormat();
	}

	@Override
	protected void onAttachedToWindow() {
		mTickerStopped = false;
		super.onAttachedToWindow();
		mHandler = new Handler();

		/**
		 * requests a tick on the next hard-second boundary
		 */
		mTicker = new Runnable() {
			public void run() {
				if (mTickerStopped)
					return;
				long currentTime = System.currentTimeMillis();
				if (currentTime / 1000 == endTime / 1000 - 5 * 60) {
					mClockListener.remainFiveMinutes();
				}
				long distanceTime = endTime - currentTime;
				distanceTime /= 1000;
				if (distanceTime == 0) {
					setText("00:00:00");
					setText("秒杀结束");
					setTextColor(new Color().rgb(153, 153, 153));
					onDetachedFromWindow();
					mClockListener.timeEnd();
				} else if (distanceTime < 0) {
//					setText("00:00:00");
//					setText("秒杀结束");                    
//					SpannableString  s=new SpannableString(" 00  :  00  :  00 ");
//					s.setSpan(new ForegroundColorSpan(Color.BLACK), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
//					s.setSpan(new ForegroundColorSpan(Color.BLACK), 11, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
//					s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//					s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//					s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 14, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//					setText(s);
					setText("00:00:00");
					setText("秒杀结束");
					setTextColor(new Color().rgb(153, 153, 153));
					onDetachedFromWindow();
				} else {
					String dealTime = dealTime(distanceTime);
					String[] split = dealTime.split(":");
					String hour=split[0].trim();
					SpannableString  s=new SpannableString(dealTime);
					if(Integer.parseInt(hour)>=100){
						s.setSpan(new ForegroundColorSpan(Color.BLACK), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
						s.setSpan(new ForegroundColorSpan(Color.BLACK), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 8, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 15, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						
					}else{
						s.setSpan(new ForegroundColorSpan(Color.BLACK), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
						s.setSpan(new ForegroundColorSpan(Color.BLACK), 11, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 14, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
					}
//					if(day==2){
//						s.setSpan(new ForegroundColorSpan(Color.BLACK), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
//						s.setSpan(new ForegroundColorSpan(Color.BLACK), 11, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
//						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 14, 18, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//					}
//					if(day==1){
/*//						s.setSpan(new ForegroundColorSpan(Color.BLACK), 6, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
//						s.setSpan(new ForegroundColorSpan(Color.BLACK), 12, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
//						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 8, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//						s.setSpan(new BackgroundColorSpan(new Color().rgb(153, 153, 153)), 15, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
*///					}
 					setText(s);
				}
				invalidate();
				long now = SystemClock.uptimeMillis();
				long next = now + (1000 - now % 1000);
				mHandler.postAtTime(mTicker, next);
			}
		};
		mTicker.run();
	}

	/**
	 * deal time string
	 * 
	 * @param time
	 * @return
	 */
	private int day;
	public static String dealTime(long time) {
		StringBuffer returnString = new StringBuffer();
		long day = time / (24 * 60 * 60);
		long hours = (time % (24 * 60 * 60)) / (60 * 60);
		long minutes = ((time % (24 * 60 * 60)) % (60 * 60)) / 60;
		long second = ((time % (24 * 60 * 60)) % (60 * 60)) % 60;
		String dayStr = String.valueOf(day);
		String hoursStr = timeStrFormat(String.valueOf(hours));
		String minutesStr = timeStrFormat(String.valueOf(minutes));
		String secondStr = timeStrFormat(String.valueOf(second));
		
		long today=System.currentTimeMillis()/(24*60*60*1000);
//		if(today>0){
//			day=1;
//			int hour=(int) (day*24+Integer.parseInt(hoursStr));
//			returnString.append(" "+hour+" ").append(" : ").append(" "+minutesStr+" ").append(" : ").append(" "+secondStr+" ");
//		}else{
//			day=2;
//			returnString.append(" "+hoursStr+" ").append(" : ").append(" "+minutesStr+" ").append(" : ").append(" "+secondStr+" ");
//		}

//		returnString.append(dayStr+"天").append(hoursStr).append(":").append(minutesStr).append(":").append(secondStr);
		int hour=(int) (day*24+Integer.parseInt(hoursStr));
		String h = "";
		if(hour<10){
			h="0"+hour;
		}else{
			h=String.valueOf(hour);
		}
		returnString.append(" "+h+" ").append(" : ").append(" "+minutesStr+" ").append(" : ").append(" "+secondStr+"  ");
		return returnString.toString();
	}

	/**
	 * format time
	 * 
	 * @param timeStr
	 * @return
	 */
	private static String timeStrFormat(String timeStr) {
		switch (timeStr.length()) {
		case 1:
			timeStr = "0" + timeStr;
			break;
		}
		return timeStr;
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		mTickerStopped = true;
	}

	/**
	 * Clock end time from now on.
	 * 
	 * @param endTime
	 */
	public void setEndTime(long endTime) {
		this.endTime = endTime;
	}

	/**
	 * Pulls 12/24 mode from system settings
	 */
	private boolean get24HourMode() {
		return android.text.format.DateFormat.is24HourFormat(getContext());
	}

	private void setFormat() {
		if (get24HourMode()) {
			mFormat = m24;
		} else {
			mFormat = m12;
		}
	}

	private class FormatChangeObserver extends ContentObserver {
		public FormatChangeObserver() {
			super(new Handler());
		}

		@Override
		public void onChange(boolean selfChange) {
			setFormat();
		}
	}

	public void setClockListener(ClockListener clockListener) {
		this.mClockListener = clockListener;
	}

	public interface ClockListener{
		void timeEnd();
		void remainFiveMinutes();
	}
	public void setText1(){
		SpannableString  s=new SpannableString("123124");
		s.setSpan(new ImageSpan(getResources().getDrawable(R.drawable.progress_dialog_bg)), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
	}

}